axios.defaults.baseURL = 'http://cba.itlike.com/public/index.php?s=/api/'

// 点击返回按钮返回上一页
document.querySelector('.back').addEventListener('click', () => history.back())


let tips = document.querySelector('.tips')
// 直接渲染随机图片验证码
async function captcha() {
    const res = await axios.get('/captcha/image', {
        header: {
            platform:'H5'
        }
    })
    console.log(res)
    document.querySelector('.picture').innerHTML = `<img src="${res.data.data.base64}" alt="这是验证码">`
    // 图片验证码的key存储本地
    localStorage.setItem('key',res.data.data.key)
}
captcha()
// 点击更改图片验证吗
document.querySelector('.picture').addEventListener('click',function () {
    captcha()
})

// 点击获取验证码
document.querySelector('.txt').addEventListener('click', async function () {
    const data = serialize(document.querySelector('form'),{hash:true,empty:true})
    console.log(data)
    const res = await axios.post('/captcha/sendSmsCaptcha', {
        headers: {
            platform:'H5'
        },
        data
    })
    // console.log(res)
    // 判断手机号是否正确
    const reg = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/
    if (!reg.test(data.mobile)) return tipss('请输入正确手机号')
    tipss('验证码:246810')
})

// 点击登录上传用户账号
document.querySelector('.login-btn').addEventListener('click',async function () {
    const data = serialize(document.querySelector('form'), { hash: true, empty: true })
    const reg = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/
    if (!reg.test(data.mobile)) return tipss('请输入正确手机号')
    if (data.captchaCode.trim() === '') return tipss('请输入验证码')
    const res = await axios({
        url:'passport/login',
        method:'post',
        data:{
          form:{
          isParty:false,
          mobile:data.mobile,
          partyData:{},
          smsCode:data.captchaKey 
          }
        }
      })
    console.log(res)
    tipss('登录成功')
    // token本地储存
    localStorage.setItem('token', res.data.data.token)
    localStorage.setItem('userId',res.data.data.userId)
    setTimeout(function () {
        location.href = './register.html'
    }, 2000)
})





// 重复调用代码封装函数
function tipss(txt) {
    tips.innerHTML = txt
    tips.style.display = 'block'
    setTimeout(function () {
    tips.style.display = 'none'
    }, 1500)
}
